﻿// See https://aka.ms/new-console-template for more information
// Console.WriteLine("Hello, World!");
// return 0;
using System.Security.Cryptography;
using System.Text;

class HMAC{
    public static void Main(){
        string secret = "ts@1682695616|route@test|sec@Abc123456|method@post";
        byte[] k = HMAC.sha512Hash(secret);
        string body = "{\"1\":222,\"2\":999,\"3\":[\"22\",1]}";
        byte[] sign = HMAC.HmacSHA256(body, k);
        string signStr = Convert.ToHexString(sign).ToLower();
        Console.WriteLine(signStr);
        return;
    }

    public static byte[] HmacSHA256(string body, byte[] signKey)
    {
        byte[] signRet;
        using (HMACSHA256 mac = new HMACSHA256(signKey))
        {
            signRet = mac.ComputeHash(Encoding.UTF8.GetBytes(body));
            // signRet = Convert.ToBase64String(hash);
            // signRet = ToHexString(hash); 
        }
        return signRet;
    }

    public static byte[] sha512Hash(string source){
        string result = string.Empty;
        byte[] buffer = Encoding.UTF8.GetBytes(source);//UTF-8 编码

        //64字节,512位
        SHA512 sha512 = SHA512.Create();
        byte[] h5 = sha512.ComputeHash(buffer);

        Console.WriteLine(BitConverter.ToString(h5, 0).Replace("-", string.Empty).ToLower());

        return h5;
    }
}
